package com.trp.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * View object wrapper,
 * @author jiangzhen
 *
 */
public abstract class GISView extends View implements ScaleListener{
	
	enum Type {
		SUBSTATION,
		POLE,
		SEGMENT,
		CABLE,
	}
	
	protected Path mGraph;
	
	protected Paint mStyle;
	
	protected boolean mIsVisible;
	
	protected Type mViewType;
	
	public GISView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public GISView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public GISView(Context context) {
		super(context);
		init();
	}
	
	private void init() {
		mGraph = new Path();
		mStyle = new Paint();
		mStyle.setColor(Color.RED);
		mStyle.setStyle(Paint.Style.STROKE);
		mIsVisible = true;
	}

	/*
	 * 
	 * @see android.view.View#onDraw(android.graphics.Canvas)
	 */
	protected void onDraw(Canvas canvas) {
		//FIXME transform every time performance is slow
		transform();
		if (isSelected()) {
			mStyle.setColor(Color.GREEN);
		} else {
			mStyle.setColor(Color.RED);
		}
		canvas.drawPath(mGraph, mStyle);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		Runnable action = null;
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			if (hitTest((int)event.getX(), (int)event.getY())) {
				setSelected(true);
				action =new Runnable() {
					@Override
					public void run() {
						notifyLongPress();
					}
				};
				postDelayed(action, 500);
				return true;
			} else {
				setSelected(false);
			}
		} else {
			if (action != null) {
				removeCallbacks(action);
			}
		}
		return false;
	}

	
	public GISView.Type getViewType() {
		return mViewType;
	}
	

	/**
	 * Test x y in this graph
	 * @param x screen x pixel not world coordination
	 * @param y screen y pixel not world coordination
	 * @return true if touched
	 */
	protected abstract boolean hitTest(int x, int y) ;
	
	
	/**
	 * When this graph is pressed more than 500misc, 
	 * then this function will be called.
	 */
	protected abstract void notifyLongPress();
	
	
	/**
	 * Sub class must set value to mGraph
	 */
	protected abstract void transform();
	
	
		
}
